<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyWord" clearable placeholder="请输入员工姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.phone" clearable placeholder="请输入联系方式"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="filters.state" clearable placeholder="员工状态">
            <el-option label="任职" value="1"></el-option>
            <el-option label="休息" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryEmp">查询</el-button>
          <el-button type="primary" @click="handleSave(null,addForm)">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleExport()">导出</el-button>
        </el-form-item>
        <el-form-item label="" prop="resource">
          <el-col :span="12">
            <el-upload
                class="upload-demo"
                action="http://localhost:8080/emp/importExcel"
                list-type="picture">
              <el-button  type="primary">点击导入</el-button>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="150" sortable>
      </el-table-column>
      <el-table-column prop="age" label="员工年龄" width="165" sortable>
      </el-table-column>
      <el-table-column prop="state" label="员工状态" width="165" sortable>
        <template slot-scope="scope">
          <div v-if="scope.row.state == 0" slot="reference" class="name-wrapper">
            <el-tag size="medium" type="danger">休息</el-tag>
          </div>
          <div v-if="scope.row.state == 1" slot="reference" class="name-wrapper">
            <el-tag size="medium">任职</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" width="250" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="250" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template scope="scope">
          <el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工姓名" prop="sn">
          <el-input v-model="saveForm.username" auto-complete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="sn">
          <el-input v-model="saveForm.age" auto-complete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="name">
          <el-input v-model="saveForm.phone" auto-complete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="name">
          <el-input v-model="saveForm.email" auto-complete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="员工状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">任职</el-radio>
            <el-radio class="radio" :label="0">休息</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      // 高级查询
      filters: {
        keyWord: '',
        phone: '',
        state: '',
      },
      // 列表
      emps: [],
      total: 0,
      currentPage: 1,// 当前页
      pageSizes: [5,10,15,20,50],
      pageSize: 10,// 页面条数
      listLoading: false,
      sels: [],//列表选中列

      // 编辑页面
      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      saveFormRules: {// 必选警告
        // name: [
        //   { required: true, message: '请输入员工姓名', trigger: 'blur' }
        // ],
      },
      //编辑界面数据
      saveForm: {
        id: null,
        username: '',
        age:'',
        state: 1,
        phone: null,
        email: null,
      },
      // 新增数据,初始化值
      addForm: {
        id: null,
        username: '',
        age:'',
        state: 1,
        phone: null,
        email: null,
      },
      saveTitle: '',
      employees: [],// 管理员
      tree: [],// 部门树
    }
  },
  methods: {
    handleExport(){
      location.href="http://localhost:8080/emp/export";
    },
    // 当前页改变触发事件
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmps();
    },
    // 最大页面条数改变触发事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.getEmps();
    },
    // 查询
    queryEmp(){
      this.currentPage = 1// 回到第一页
      this.getEmps()
    },
    // 重置搜索框
    resetForm(filters) {
      this.$refs['filters'].resetFields();
    },
    // 获取用户列表
    getEmps() {
      let para = {
        currentPage: this.currentPage,
        pageSize:this.pageSize,
        keyWord: this.filters.keyWord,
        phone: this.filters.phone,
        state: this.filters.state,
      };
      // 开启忙等
      this.listLoading = true;
      // 获取分页数据
      this.$http.post("/emp/query",para).then(res=>{
        this.emps = res.data.data
        this.total = res.data.total
        this.listLoading = false;
      })
      // 获取部门列表
      // this.$http.get("/emp/list",{}).then(res=>{
      //   this.employees = res.data.data
      // })
      // 获取部门树
      // this.$http.get("/emp/tree").then(res => {
      //   this.tree = res.data
      // })
    },

    // 显示编辑界面
    handleSave: function (index, row) {
      if(row.id != null){
        this.saveTitle = '修改员工信息'// id不为空，修改
      }else{
        //this.saveForm = Object.assign({},row)
        this.saveTitle = '新增员工信息'// id为空，添加
      }
      this.saveForm = Object.assign({}, row);// 初始化，克隆，拷贝
      this.saveFormVisible = true;// 关闭对话框
      // 重置表单验证
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();
      }
    },

    //保存
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = false;// 忙等
            // 克隆，如果使用赋值会触发双向绑定，在表单中修改数据，即使不保存也会修改列表中显示的值
            let para = Object.assign({}, this.saveForm);
            // 如果父类id为不为空，将部门树集合的最后一个值赋值给para的父类id，将值传入后端
            // if(para.parent_id){
            //   // 判断为对象，将对象中最后一级赋值回去，传入后端
            //   if (typeof para.parent_id == "object"){
            //     para.parent_id = para.parent_id[para.parent_id.length - 1]
            //   }
            // }
            // 请求
            this.$http.put("/emp",para).then((res) => {
              this.saveLoading = false;
              if(res.data.state){
                this.$message({
                  message: res.data.tip,
                  type: 'success'
                });
                this.$refs['saveForm'].resetFields();//重置表单验证
                this.saveFormVisible = false;// 关闭模态框
                // 修改完后刷新列表，但不返回第一页
                // 通常查询删除返回第一页，修改新增停留在当前页
                this.getEmps()
              }else {
                this.$message({
                  message: res.data.tip,
                  type: 'warning'
                });
              }
            }).catch(()=>{
              this.$message({
                message: "已取消保存",
              });
            });

          });
        }
      });
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;// 关闭盲等
        this.$http.delete("/emp/"+row.id).then(res => {
          if(res.data.state){
            this.$message({
              message: res.data.tip,
              type: 'success'
            });
          }else {
            this.$message({
              message: res.data.tip,
              type: 'warning'
            });
          }
          this.currentPage = 1// 回到第一页
          this.queryEmp()
        });
      }).catch(() => {
        this.$message({
          message: "已取消删除",
        });
      });
    },

    // 选择框改变
    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      console.log(ids)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 批量删除
        this.$http.patch("/emp",ids).then(res=>{
          if(res.data.state){
            this.$message({
              message: res.data.tip,
              type: 'success'
            });
          }else {
            this.$message({
              message: res.data.tip,
              type: 'warning'
            });
          }
          this.queryEmp()
        })
      }).catch(() => {
        this.$message({
          message: "已取消删除",
        });
      });
    }


  },
  mounted() {
    this.getEmps()
  }
}

</script>

<style scoped>

</style>